package com.waimai.mobile.ui.activity;


import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.widget.Toast;

import com.waimai.mobile.R;

import com.waimai.mobile.ui.fragment.HomeFragment;
import com.waimai.mobile.ui.fragment.HomeFragment_;
import com.waimai.mobile.ui.fragment.MainTabFragment;
import com.waimai.mobile.ui.fragment.OrderListFragment;
import com.waimai.mobile.ui.fragment.OrderListFragment_;
import com.waimai.mobile.ui.fragment.UserCenterFragment;
import com.waimai.mobile.ui.fragment.UserCenterFragment_;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.FragmentById;
import org.androidannotations.annotations.OnActivityResult;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

@EActivity(R.layout.activity_main)
public class MainActivity extends BaseActivity01 {



    @ViewById
    public ViewPager mainViewPager;

    @FragmentById(R.id.tabMainFragment)
    public MainTabFragment tabMainFragment;


    @AfterViews
    public void afterViews(){

        final List<Fragment> fragments = new ArrayList<Fragment>();
        HomeFragment homeFragment = new HomeFragment_();
        OrderListFragment orderListFragment = new OrderListFragment_();
        UserCenterFragment userCenterFragment = new UserCenterFragment_();

        fragments.add(homeFragment);
        fragments.add(orderListFragment);
        fragments.add(userCenterFragment);
        mainViewPager.setOffscreenPageLimit(0);
        mainViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });

        tabMainFragment.setOnTabSelectedListtener(new MainTabFragment.OnTablSelectedListener() {
            @Override
            public void onTablSelected(int position, ViewGroup view) {
                switch (position) {
                    case 0:
                        setCurrentPage(position);
                        mainViewPager.setCurrentItem(position, true);
                        break;
                    case 1:
                        setCurrentPage(position);
                        mainViewPager.setCurrentItem(position, true);
                        break;
                    case 2:
                        setCurrentPage(position);
                        mainViewPager.setCurrentItem(position, true);
                        break;
                }
            }
        });

        mainViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setCurrentPage(position);
                tabMainFragment.selectItem(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    private boolean isExit = false;
    //退出操作
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            if(isExit==false){
                isExit=true;
                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
                handler.sendEmptyMessageDelayed(0, 1500);
                return true;
            } else {
                finish();
                return false;
            }
        }
        return true;
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit=false;
        }
    };
}
